package MultiThreaded;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.BlockingQueue;
/**
 * Analyzing the run time of the parser, and analyzing the keywords found.
 * @author zsyed
 *
 */
public class PageAnalyzer implements Runnable {

    private volatile boolean is_finished = false;
    private volatile boolean is_waiting = false;
	private int my_url_count;
	private int my_word_count;
	private HashMap<String, Integer> my_keywords = new HashMap<String, Integer>();
	private boolean poisoned1;
	private boolean poisoned2;
	private  BlockingQueue<List<String>> my_content;
	private  BlockingQueue<List<String>> my_embedded_urls;

	public PageAnalyzer(BlockingQueue<List<String>> content,
			BlockingQueue<List<String>> embedded_urls,
			HashMap<String, Integer> keywords) {
		my_content = content;
		my_embedded_urls = embedded_urls;
		my_keywords = keywords;
	}
	


	/**
	 * This thread will run until page parser poisons it twice.
	 */
	 @Override
	 public void run() {

		while (!poisoned1 || !poisoned2) {

	        //System.out.println("Analyzing URLs!");
			analyzeUrls();

	        //System.out.println("Analyzing content!");
			analyzeContent();
	        //System.out.println("Analyzed!");
		}
		MultiThreadSetup.avg_urls_per_page = (double)my_url_count / (double)MultiThreadSetup.page_counter;
		MultiThreadSetup.avg_words_per_page = (double)my_word_count / (double)MultiThreadSetup.page_counter;
		is_finished = true;
		//System.out.println("Finished analyzing");
	 }

	 /**
	  * Count urls, checks for poison.
	  */
	 private void analyzeUrls() {
		 List<String> urls = new ArrayList<String>();
		 try {
		     is_waiting = true;
			 urls = my_embedded_urls.take();
			 is_waiting = false;
		 } catch (InterruptedException e1) {
			 return;
		 }
		 if (urls.contains("POISON")) {
			 poisoned1 = true;
		 }
		 else {
			 my_url_count += urls.size();
		 }
	 }
	 /**
	  * Counts the number of words, checks for poison, and also
	  * calls method to count keywords if content is valid.
	  */
	 private void analyzeContent() {
		 List<String> content = new ArrayList<String>();
		 try {
		     is_waiting = true;
			 content = my_content.take();
			 is_waiting = false;
		 } catch (InterruptedException e) {
			 return;
		 }

		 if (content.contains("POISON")) {
			 poisoned2 = true;
		 }
		 else {
			 my_word_count += content.size();
			 countKeyWords(content);
		 }
	 }
	 /**
	  * Count the keywords in the content.
	  * @param content The content of the source.
	  */
	 private void countKeyWords(List<String> content) {
		 for (int i = 0; i < content.size(); i++) {
		     //System.out.println(content.get(i).toLowerCase());
			 String word = content.get(i).toLowerCase();
			 if (my_keywords.containsKey(word)) {
				 int count = my_keywords.get(word);
				 my_keywords.put(word, count+1);
			 }
		 }
	 }
	 
	 /**
	  * returns whether or not the analyzer is waiting for another process to put something into its
	  * queue.
	  * @return
	  */
	 public boolean getIsWaiting(){
	     return is_waiting;
	 }
     
	 /**
	  * Returns whether or not the page analyzer has ended its process (i.e., has been poisoned).
	  * @return whether it's finished.
	  */
     public boolean getIsFinished(){
         return is_finished;
     }

}
